var appPath = getAppPath();	

$(function(){
	/**
	 * 查询数据条件
	 */

	initJspGrid();

});

// 获取分类信息
function getTopTypeNm(value){
	var tn = '';
	switch(Number(value||'0')){
  	case 1: tn='单页';break;
  	case 2: tn='多页';break;
  	case 3: tn='PPT';break;
  	case 4: tn='<span style="color:red">印品</span>';break;
  	case 6: tn='社交媒体';break;
  	case 7: tn='电商';break;
  	case 8: tn='微商';break;
  	case 1010: tn='Logo商品';break;
  	case 1011: tn='营销商品';break;
  	case 1030: tn='视频背景';break;
  }
  return tn;
}

// 初始化
function initJspGrid(){
	
	$('#tmplGrid').datagrid({
	    url:appPath + "/itbSearch/getSearchTagInfoPg.action",
	    height:'full',
	    //remoteSort:false,
	    striped:true,
	    fitColumns:true,
	    queryParams: getQueryParam(),
	    singleSelect:false,
	    // If set to true, clicking a checkbox will always select the row. 
	    // If false, selecting a row will not check the checkbox.
	    selectOnCheck:false,
	    // If true, the checkbox is checked/unchecked when the user clicks on a row. 
	    // If false, the checkbox is only checked/unchecked when the user clicks exactly on the checkbox.
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
			//微缩图
	    		{field:'edit',title:'编辑',width:35,align:'center',sortable:false,
					formatter:function(value,rowData,rowIndex){
						var ret = "<a href='javascript:void(0)'><img width='12px' height='12px' style='border:0px' src='"+appPath+"/js/lib/jquery-easyui/themes/icons/pencil.png' title='编辑' ";
						ret += " onClick='editSearch("+rowData.tag_id+")'/></a>";
						return ret;
					}
				},
		        {field:'tag_id',title:'id',width:60,align:'left',sortable:false},
		        {field:'name',title:'关键词',width:60,align:'left',sortable:false},
		        {field:'type',title:'分类',width:60,align:'left',sortable:false,
			    	  formatter:function(value, rowData, rowIndex){
			    		  return getTopTypeNm(value);
			    	  }
		        },
			    {field:'order',title:'序号',width:40,align:'center',sortable:false,
			   	  formatter:function(value, rowData, rowIndex){
			    		  if(!rowData.tag_id) return "";
			    		  return "<a href='javascript:void(0)' onClick='searchTagOrder("+rowData.tag_id+","+rowData.order+")'>"+rowData.order+"</a>";
			    	  }
			    },

			    {field:'create_time',title:'创建时间',width:60,left:'center',sortable:false,
			    	  formatter:function(value, rowData, rowIndex){
			    		  if(value&&value!=0){
			    			  return value.substr(0,19);
			    		  }
			    		  return 'N/A'
			    	  }
			    },
			    {field:'update_time',title:'修改时间',width:60,left:'center',sortable:false,
			    	  formatter:function(value, rowData, rowIndex){
			    		  if(value&&value!=0){
			    			  return value.substr(0,19);
			    		  }
			    		  return 'N/A'
			    	  }
			    }
			    
	    ]],
	    hideColumn:['user_type'],
	    //view: imgView,// 自定义View
	    pagination:true,
		rownumbers:true,
		toolbar:[{
			text:'新建关键词',
			iconCls:'icon-add',
			handler:function(){
				addSearch();
			}
		},
		{
			text:'删除关键词',
			iconCls:'icon-cancel',
			handler:function(){
				delSerach();
			}
		}
		]	
	});
}



function closeMsgList(){
	$('#editMessage').window('close');
}
//编辑
function editSearch(stId){	
	$("#editTable").show();
	$('#editForm').form('clear');
	$("#topicImg").html('').val('');
	
	$("#trForNew").hide();
	$("#trForEdit").show();
	$("#showTypeForEdit").html('');
	
	$("#editStId").val(stId);
	var url = appPath + "/itbSearch/getSearchInfoEdit.action?id="+ stId;
	$.get(url,function(ret){
		if (ret && ret.length>0){
			var info = ret[0];
			var type = Number(info.type||0);
		
			$("#editSearchTitle").val(info.name||'');
			$("#editSearchUse").combobox('select',info.type||'');//大的分类
			$("#SearchType").val(type);
			if(type>0) {
				var show = '';
				switch(Number(type)){
					case 1: show = '海报';break;
					case 2: show = '多页';break;
					case 3: show = 'PTT';break;
					case 4: show = '印品';break;
					case 6: show = '社交媒体';break;
					case 7: show = '电商';break;
					
				}
			}
		}
	});
	
	$("#editBtn").show();
	$('#editDlg').window({
		title:'编辑关键字',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#editDlg').window('move',{top:20,left:20});
	$('#editDlg').window('open');
}

//添加
function addSearch(){
	$("#editTable").show();
	$('#editForm').form('clear');

	$("#editBtn").show();
	$('#editDlg').window({
		title:'新建关键字',
		iconCls:'icon-add',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#editDlg').window('move',{top:20,left:20});
	$('#editDlg').window('open');
}
function doEdit(){
	var stId = $("#editStId").val();
	var type = $("#editSearchUse").combobox('getValue');
	var name = $("#editSearchTitle").val();
	if(Number(type||0)<=0){
		return $.messager.alert('提示信息','请选择消息分类','info');
	}
	if(name&& name.length>0) {
		var url = appPath+"/itbSearch/addSearch.action"
		var sendData = {
			"searchInfo.tag_id": stId||"",
			"searchInfo.type": Number(type),
			"searchInfo.name": name || ""

		};
	    $.post(url, sendData, function(data){  
			if (data == "ok") { 
				$.messager.alert('提示信息','提交成功','info');
				$('#editDlg').window('close');
				doQuery();
	        } else {
	        	$.messager.alert('提示信息','提交失败','info');
	        }
	    });
	    
	}else{
		$.messager.alert('提示信息','标题名称、有效起始、截止日期时间是否填写','info');
	}
	
}

// 删除
function delSerach(){
	var selectItem=$("#tmplGrid").datagrid("getChecked");//获取勾选框
	if(!selectItem || selectItem.length == 0){
		$.messager.alert('提示信息','请选择删除项目！','warning');
		return;
	}

	$.messager.confirm("确认", "<span style='color:red'>确认删除所选［搜索关键字］？</span>",
		function(r){
	        if (r) { 
				var ids = "";
				$.each(selectItem, function(index, item){
					ids = ids + item.tag_id + ",";
				});
				var url = appPath+"/itbSearch/delSearch.action?ids="+ids;
				$.post(url,function(data){
					if(data=="ok"){
						$.messager.alert('提示信息','删除成功','info');
						doQuery();
					}else{
						$.messager.alert('提示信息','删除失败','info');
					}
				});
	        }
		}
	);
}


/**
 * 显示顺序
 */

function searchTagOrder(id,order){
	$("#searchOrderId").val(id);
	$("#searchOrder").val(order);
	
	$("#searchBtn").show();
	$('#searchTagDlg').window({
		title:'显示顺序设定',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#searchTagDlg').window('move',{top:20,left:20});
	$('#searchTagDlg').window('open');
}
/**
 * 修改显示顺序
 */
function doUpdSearch(){
	var id = $("#searchOrderId").val();
	var order = $("#searchOrder").val();
	if(!order || order.length == 0 || Number(order)>999 || Number(order)<=0){
		$.messager.alert('提示信息','有效数值范围为：1～999','warning');
		return;
	}
	
	var url = appPath+"/itbSearch/updSearchOrder.action?orderid="+id+"&order="+order;
	$.post(url,function(data){
		if(data=="ok"){
			$.messager.alert('提示信息','显示顺序已更新！','info',function(){
				doQuery();
				$('#searchTagDlg').window('close');
			});
		}else {
			$.messager.alert('提示信息','显示顺序更新失败！','warning');
		}
	});
}
/**
 * 结束显示顺序编辑
 */
function cancelUpdSearch(){
	$('#searchTagDlg').window('close');
}

function cancelEdit(){
	$('#editDlg').window('close');
}

//查询参数条件
function getQueryParam(){
    
	var queryObject = {};
	
	var findType= $("#searchTagType").combobox('getValue');//获取类型			

	var findTitleName=$("#queryTitleName").val().trim();//标题名字
	

	if(findType && Number(findType||'0')>0) queryObject.searchTagType = findType;// 类型

	if(findTitleName&&findTitleName.length>0) queryObject.findTitleName = findTitleName;// 样版名称

	return queryObject;
}

/**
 * 查询数据
 */
function doQuery(){
	$("#tmplGrid").datagrid('unselectAll');
	$("#tmplGrid").datagrid('load',getQueryParam());
	
}




